//
//  UIScreen+SGExtension.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/12/12.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import UIKit

extension UIScreen: SpadgerCompatible {}

// MARK: - 基本的扩展方法
public extension Spadger where Base == UIScreen {
   
    /// 监听截屏或者录屏通知
    /// - Parameter action: 事件
    static func listenScreenShot(_ action: @escaping (String) -> Void) {
        let mainQueue = OperationQueue.main
        NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { _ in
            action("截屏")
        }
        //监听录屏通知,iOS 11后才有录屏
        if #available(iOS 11.0, *) {
            //如果正在捕获此屏幕（例如，录制、空中播放、镜像等），则为真
            if UIScreen.main.isCaptured {
                action("录屏")
            }
            //捕获的屏幕状态发生变化时,会发送UIScreenCapturedDidChange通知,监听该通知
            NotificationCenter.default.addObserver(forName: UIScreen.capturedDidChangeNotification, object: nil, queue: mainQueue) { _ in
                action("录屏")
            }
        }
    }
    
}
